package com.geese.common.util;

import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import com.alibaba.fastjson.serializer.ValueFilter;


public abstract class JSONUtil {
	
	private static final ValueFilter NULL_VALUE_FILTER  = new JsonValueFilter();
	private static SerializeConfig mapping = new SerializeConfig(); 
	private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
	
	static {
		mapping.put(Date.class, new SimpleDateFormatSerializer(DEFAULT_DATE_FORMAT));
	}
	
	public static String objectToJson(Object obj) {
		return JSON.toJSONString(obj, mapping, NULL_VALUE_FILTER);
	}
	
	public static <T> T parse(String str, Class<T> clazz) {
		return JSON.parseObject(str, clazz);
	}
	
    public static class JsonValueFilter implements ValueFilter {

		@Override
        public Object process(Object obj, String s, Object v) {
            if (v == null)
                return "";
            return v;
        }
	}
      
}
